using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Microsoft.CommerceServer.Catalog;
using System.Configuration;
using Microsoft.CommerceServer;

namespace CatalogManagerDAL
{
    public static class DataHelper
    {
        public static DataTable FilterTable(DataTable DataTableToFilter, string FilterString)
        {
            DataRow[] filteredRows = DataTableToFilter.Select(FilterString);
            DataTable FilteredTable = DataTableToFilter.Clone();
            foreach (DataRow drow in filteredRows)
            {
                FilteredTable.ImportRow(drow);
            }
            FilteredTable.AcceptChanges();
            return FilteredTable;
        }

        public static CatalogContext InitContextUsingSiteAgent()
        {
            CatalogSiteAgent siteInfo = new CatalogSiteAgent();
            siteInfo.SiteName = "StarterSite";
            siteInfo.AuthorizationMode = AuthorizationMode.ThreadContext;
            siteInfo.AuthorizationPolicyPath = @"E:\Inetpub\wwwroot\CatalogWebService\CatalogAuthorizationStore.xml";
            siteInfo.IgnoreInventorySystem = false;
            CacheConfiguration cacheConfiguration = new CacheConfiguration();
            cacheConfiguration.CacheEnabled = true;
            cacheConfiguration.SchemaCacheTimeout = new TimeSpan(0, 10, 0);
            return CatalogContext.Create(siteInfo, cacheConfiguration);
        }

        public static CatalogServiceAgent InitServiceAgent()
        {
            string Url = ConfigurationManager.AppSettings["WebService_Url"];
            string UserName = ConfigurationManager.AppSettings["WebService_UserName"];
            string Password = ConfigurationManager.AppSettings["WebService_Password"];
            string Domain = ConfigurationManager.AppSettings["WebService_Domain"];

            CatalogServiceAgent agent1 = new CatalogServiceAgent(
                Url, ServiceAgent.DefaultAuthMethods
            );

            if (!string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(Password))
            {
                agent1.Credentials = new System.Net.NetworkCredential(UserName, Password, Domain);
            }
            agent1.AllowBasicOverNonSecure = true;

            return agent1;
        }

        public static CatalogContext InitContext()
        {
            CatalogContext ctx = CatalogContext.Create(
                InitServiceAgent()
            );
            return ctx;
        }

        public enum PropertyDataType
        {
            /// <summary>
            /// Integer
            /// </summary>
            Integer = 0,

            /// <summary>
            /// BigInteger 
            /// </summary>
            BigInteger = 1,

            /// <summary>
            /// Float
            /// </summary>
            Float = 2,
            /// <summary>
            /// Double
            /// </summary>
            Double = 3,

            /// <summary>
            /// Boolean 
            /// </summary>
            Boolean = 4,

            /// <summary>
            /// String 
            /// </summary>
            String = 5,
            /// <summary>
            /// DateTime 
            /// </summary>
            DateTime = 6,
            /// <summary>
            /// Currency 
            /// </summary>
            Currency = 7,
            /// <summary>
            /// FilePath 
            /// </summary>
            FilePath = 8,
            /// <summary>
            /// Enumeration 
            /// </summary>
            Enumeration = 9,
            /// <summary>
            /// Text 
            /// </summary>
            Text = 10,
            /// <summary>
            /// MultiValue 
            /// </summary>
            MultiValue = 11,
            /// <summary>
            /// CallbackEvent 
            /// </summary>
            CallbackEvent = 12,
            /// <summary>
            /// MultilingualEnumeration 
            /// </summary>
            MultilingualEnumeration = 13,
            /// <summary>
            /// Guid
            /// </summary>
            Guid = 14,

            /// <summary>
            /// Date 
            /// </summary>
            Date = 15,

            /// <summary>
            /// Time
            /// </summary>
            Time = 16,
            /// <summary>
            /// Custom1 
            /// </summary>
            Custom1 = 17,
            /// <summary>
            /// Custom2 
            /// </summary>
            Custom2 = 18,
            /// <summary>
            /// Password 
            /// </summary>
            Password = 19
        }

        public static void AddVariant(ProductFamily productFamily, string variantId,
            string VariantProperty1, string VariantProperty2, string VariantProperty3, string VariantProperty4,
            decimal VariantPurchasePrice, decimal VariantListPrice, string VariantBarcode, int VariantDesi,
            int OnHandQuantity, short Status
            )
        {
            // Add the variant to the product family. 
            productFamily.AddVariant(variantId);

            // Set the properties for the variant product.
            productFamily.Variants[variantId]["VariantProperty1"] = VariantProperty1;
            productFamily.Variants[variantId]["VariantProperty2"] = VariantProperty2;
            productFamily.Variants[variantId]["VariantProperty3"] = VariantProperty3;
            productFamily.Variants[variantId]["VariantProperty4"] = VariantProperty4;
            productFamily.Variants[variantId]["VariantPurchasePrice"] = VariantPurchasePrice;
            productFamily.Variants[variantId]["VariantListPrice"] = VariantListPrice;
            productFamily.Variants[variantId]["VariantBarcode"] = VariantBarcode;
            productFamily.Variants[variantId]["VariantDesi"] = VariantDesi;

            productFamily.Variants[variantId]["OnHandQuantity"] = OnHandQuantity;
            productFamily.Variants[variantId]["Status"] = Status;
        }

        public static string TrimBeforeSecondChar(string Word, string Character)
        {
            Word = Word.Substring(Word.IndexOf(Character) + 1);
            Word = Word.Substring(Word.IndexOf(Character) + 1);
            return Word;
        }
    }
}
